package com.huikeportal.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huikeportal.controller.common.Lang;
import com.huikeportal.controller.common.MessageManager;
import com.huikeportal.controller.common.Tools;
import com.huikeportal.model.ChangeGcParam;
import com.huikeportal.model.Language;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SquareChange;
import com.huikeportal.model.TreeHtml;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;
import com.huikeportal.service.UserService;

/**
 * 用户管理
 * 
 * @author XIAKUN
 * 
 */
@Controller
@Scope("prototype")
public class UserController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private UserService userService;

	@Autowired
	private SelectSiteService selectSiteService;

	@InitBinder("userManagerInfo")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userManagerInfo.");
	}

	@InitBinder("pageDataVo")
	public void initBinderPage(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("pageDataVo.");
	}

	/**
	 * 用户管理
	 * 
	 * @param userManagerInfo
	 * @param pageDataVo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userManager")
	public String userManager(UserManagerInfo userManagerInfo,
			PageDataVo pageDataVo, ModelMap model) throws Exception {
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 如果下拉框为请选择，则置空
		if ("-1".equals(userManagerInfo.getUserStatus())) {
			userManagerInfo.setUserStatus(null);
		}
		if ("-1".equals(userManagerInfo.getRoleId())) {
			userManagerInfo.setRoleId(null);
		}
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取用户角色下拉框信息
		List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
		// 获取用户状态下拉框信息
		List<WordBook> statusList = userService.getUserStatusCombo(sysLang);
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		// 如果是初始化，默认显示第一页
		if (pageDataVo.getCurrentPage() == 0) {
			pageDataVo.setCurrentPage(1);
		}
		// 获取每页显示条数
		pageDataVo.setPageSize(Integer.parseInt(MessageManager
				.getMsg("pageSize")));
		// 设置开始显示条数
		pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
				* pageDataVo.getPageSize());
		// 设置参数
		userManagerInfo.setId(userInfo.getId());
		pageDataVo.setSearchParameter(userManagerInfo);
		// 获取当前页用户信息
		pageDataVo = userService.getUserList(pageDataVo);
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 角色下拉框数据
		model.addAttribute("statusList", statusList);// 用户状态下拉框数据
		model.addAttribute("pageDataVo", pageDataVo);// 用户列表分页信息
		model.addAttribute("recentUserName", userInfo.getUserName());
		return "userManager";
	}

	/**
	 * ajax查询用户
	 * 
	 * @param roleId
	 * @param status
	 * @param userName
	 * @param currentPage
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/ajaxUserList")
	public Map<String, Object> ajaxUserList(String roleId, String status,
			String userName, String currentPage) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		UserManagerInfo userManagerInfo = new UserManagerInfo();
		// 如果下拉框为请选择，则置空
		if (status != null && !"-1".equals(status) && !"".equals(status)) {
			userManagerInfo.setUserStatus(status);
		}
		if (roleId != null && !"-1".equals(roleId) && !"".equals(roleId)) {
			userManagerInfo.setRoleId(roleId);
		}
		if (userName != null && !"".equals(userName)
				&& !"undefined".equals(userName)) {
			userManagerInfo.setRealName(URLDecoder.decode(userName, "UTF-8"));
		}
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		PageDataVo pageDataVo = new PageDataVo();
		// 默认显示第一页
		if (currentPage == null || "".equals(currentPage)) {
			pageDataVo.setCurrentPage(1);
		} else {
			pageDataVo.setCurrentPage(Integer.parseInt(currentPage));
		}
		// 获取每页显示条数
		pageDataVo.setPageSize(Integer.parseInt(MessageManager
				.getMsg("pageSize")));
		// 设置开始显示条数
		pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
				* pageDataVo.getPageSize());
		// 设置参数
		userManagerInfo.setId(userInfo.getId());
		pageDataVo.setSearchParameter(userManagerInfo);
		String isManager = "0";
		// 判断当前登录用户是否是管理员
		if (userInfo.getCreateUserId() == null
				|| "".equals(userInfo.getCreateUserId())) {
			isManager = "1";
		} else {
			isManager = "0";
		}
		// 获取当前页用户信息
		pageDataVo = userService.getUserList(pageDataVo);
		map.put("language", language);// 系统语言包
		map.put("userList", pageDataVo.getPageResult());// 结果集
		map.put("currentPage", pageDataVo.getCurrentPage());// 当前页
		map.put("maxPage", pageDataVo.getMaxPage());// 最大页
		map.put("recentUserName", userInfo.getUserName());// 当前登录用户名
		map.put("isDelete", language.getDelete());// 是否有删除权限
		map.put("isManager", isManager);// 是否是管理员用户
		return map;
	}

	/**
	 * 按钮过滤
	 * 
	 * @param language
	 *            语言包
	 * @param buttonList
	 *            按钮权限
	 * @return
	 * @throws Exception
	 */
	private Language filterLanguage(Language language, List<WordBook> buttonList)
			throws Exception {
		boolean add = false;
		boolean modify = false;
		boolean delete = false;
		for (int i = 0; i < buttonList.size(); i++) {
			if ("2".equals(buttonList.get(i).getDicCode())) {
				add = true;
			}
			if ("3".equals(buttonList.get(i).getDicCode())) {
				modify = true;
			}
			if ("4".equals(buttonList.get(i).getDicCode())) {
				delete = true;
			}
		}
		if (!add) {
			language.setAdd("");
		}
		if (!modify) {
			language.setModify("");
		}
		if (!delete) {
			language.setDelete("");
		}
		return language;
	}

	/**
	 * 新增用户初始化
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userAddLoad")
	public String userAddLoad(ModelMap model) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前用户集团权限
		List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
		// 获取当前用户地域权限
		List<SiteAllInfo> dyList = loginService.getDySiteList(userInfo);
		// 获取当前用户广场权限
		List<SiteAllInfo> gcList = loginService.getDpGcSiteList(userInfo);
		List<SiteAllInfo> xianzaiList = loginService.getGcSiteList(userInfo);
		for (int i = 0; i < gcList.size(); i++) {
			if (!Tools.isHaveGcPer(xianzaiList, gcList.get(i).getSiteid())) {
				gcList.get(i).setSitestaus(0);
			} else {
				gcList.get(i).setSitestaus(1);
			}
		}
		// 获取用户角色下拉框信息
		List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
		// // 获取数据类型信息
		// SqlParameter sql = new SqlParameter();
		// sql.setString(userInfo.getUserId());
		// sql.setSysLang(sysLang);
		// List<WordBook> dataList = userService.getDataPermission(sql);
		// 获取数据类型（新版，2016年1月11日10:42:50）
		Map<String, Object> indexMap = userService.selectUserDateType(userInfo
				.getUserId());
		/****** 筛选广场初始化 *****/
		List<SiteAllInfo> dpGcList = loginService.getDpGcSiteList(userInfo);
		ChangeGcParam changeGcParam = new ChangeGcParam();
		changeGcParam.setSelectType("1");// 多选场所
		List<SiteAllInfo> yuanGcList = loginService.getGcSiteList(userInfo);
		SquareChange squareChange = selectSiteService.squareChangeLoad(
				changeGcParam, dpGcList, yuanGcList);// 获取初始化更换广场信息
		model.addAttribute("squareChange", squareChange);// 更换广场数据初始化
		/****** 筛选广场初始化 *****/
		session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 角色下拉框数据
		// model.addAttribute("dataList", dataList);// 数据权限复选框
		model.addAttribute("dataTypeList", JSONArray.fromObject(indexMap
				.get("dataTypeList")));// 数据类型
		model.addAttribute("dataTypeMap", indexMap.get("dataTypeList"));
		model.addAttribute("indexList", JSONArray.fromObject(indexMap
				.get("indexList")));// 指标
		model.addAttribute("jtList", jtList);// 集团列表
		model.addAttribute("dyList", dyList);// 集团列表
		model.addAttribute("gcList", gcList);// 集团列表
		return "userManagerAdd";
	}

	/**
	 * 新增用户提交
	 * 
	 * @param userManagerInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userAddSub")
	public String userAddSub(UserManagerInfo userManagerInfo, ModelMap model)
			throws Exception {
		// 判断是否重复提交
		boolean flag = userService.selectUserExists(userManagerInfo
				.getUserName());
		MessageCommon messageCommon = new MessageCommon();
		if (flag) {
			// 设置当前登录用户为创建者
			userManagerInfo.setCreateUserId(session.getAttribute("userName")
					.toString());
			messageCommon = userService.addUser(userManagerInfo);
			if (!messageCommon.isFlag()) {
				return "error";
			}
		} else {
			messageCommon.setMessage("不能重复提交！");
		}
		// 返回一览
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取用户角色下拉框信息
		List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
		// 获取用户状态下拉框信息
		List<WordBook> statusList = userService.getUserStatusCombo(sysLang);
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		PageDataVo pageDataVo = new PageDataVo();
		// 如果是初始化，默认显示第一页
		if (pageDataVo.getCurrentPage() == 0) {
			pageDataVo.setCurrentPage(1);
		}
		// 获取每页显示条数
		pageDataVo.setPageSize(Integer.parseInt(MessageManager
				.getMsg("pageSize")));
		// 设置开始显示条数
		pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
				* pageDataVo.getPageSize());
		// 设置参数
		userManagerInfo = new UserManagerInfo();
		userManagerInfo.setId(userInfo.getId());
		pageDataVo.setSearchParameter(userManagerInfo);
		// 获取当前页用户信息
		pageDataVo = userService.getUserList(pageDataVo);
		session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 角色下拉框数据
		model.addAttribute("statusList", statusList);// 用户状态下拉框数据
		model.addAttribute("pageDataVo", pageDataVo);// 用户列表分页信息
		model.addAttribute("userManagerInfo", userManagerInfo);
		model.addAttribute("message", messageCommon.getMessage());
		model.addAttribute("recentUserName", userInfo.getUserName());
		return "userManager";
		// // 返回新增页面
		// else {
		// UserInfo userInfo = (UserInfo) session
		// .getAttribute("sessionUserName");
		// // 获取当前系统语言
		// String sysLang = session.getAttribute("sysLang").toString();
		// String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
		// // 获取用户信息
		// userInfo = loginService.getUserInfo(userInfo);
		// // 获取当前用户集团权限
		// List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
		// // 获取当前用户地域权限
		// List<SiteAllInfo> dyList = loginService.getDySiteList(userInfo);
		// // 获取当前用户广场权限
		// List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
		// if (gcList.size() == 0) {
		// model.addAttribute("isHaveGc", "0");
		// } else {
		// model.addAttribute("isHaveGc", "1");
		// }
		// if (jtList.size() != 0) {
		// model.addAttribute("jtSiteInfo", jtList.get(0));
		// } else {
		// model.addAttribute("jtSiteInfo", "");
		// }
		// if (dyList.size() != 0) {
		// model.addAttribute("dyList", dyList);
		// } else {
		// model.addAttribute("dyList", "");
		// }
		// // 获取用户角色下拉框信息
		// List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
		// // 获取报告权限信息
		// // List<SiteAllInfo> reportList = userService
		// // .getReportPermission("hkcloud_report_type");
		// // 获取数据类型信息
		// SqlParameter sql = new SqlParameter();
		// sql.setString(userInfo.getUserId());
		// sql.setSysLang(sysLang);
		// List<WordBook> dataList = userService.getDataPermission(sql);
		// session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
		// model.addAttribute("roleList", roleList);// 角色下拉框数据
		// // model.addAttribute("reportList", reportList);// 报告权限复选框
		// model.addAttribute("dataList", dataList);// 数据权限复选框
		// model.addAttribute("userManagerInfo", userManagerInfo);
		// return "userManagerAdd";
		// }
	}

	/**
	 * 编辑用户初始化
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userEditLoad")
	public String userEditLoad(UserManagerInfo userManagerInfo, ModelMap model)
			throws Exception {
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
		// 获取登录用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取用户角色下拉框信息
		List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
		// 获取当前编辑用户信息
		userManagerInfo = userService.getUserById(userManagerInfo);
		// 集团
		if (!"".equals(userManagerInfo.getJtSiteId())
				&& userManagerInfo.getJtSiteId() != null) {
			model.addAttribute("jtStringId", userManagerInfo.getJtSiteId());
		} else {
			model.addAttribute("jtStringId", "");
		}
		// 仅集团
		model.addAttribute("jinjtStringId", userManagerInfo.getJinjituan());
		// 地域
		if (!"".equals(userManagerInfo.getDySiteId())
				&& userManagerInfo.getDySiteId() != null) {
			model.addAttribute("dyStringId", userManagerInfo.getDySiteId());
		} else {
			model.addAttribute("dyStringId", "");
		}
		// 仅地域
		model.addAttribute("jindyStringId", userManagerInfo.getJindiyu());
		// 拼接广场、店铺选择
		List<SiteAllInfo> gccList = userService.getSiteByString(userManagerInfo
				.getGcSiteId());
		List<SiteAllInfo> dpList = userService.getSiteByString(userManagerInfo
				.getDpSiteId());
		if (gccList.size() != 0) {
			model.addAttribute("gcUserList", Tools.pinjieId(gccList));
			model.addAttribute("gcNameString", Tools.pinjieName(gccList));
		}
		if (dpList.size() != 0) {
			model.addAttribute("dpUserList", Tools.pinjieId(dpList));
			model.addAttribute("dpNameString", Tools.pinjieName(dpList));
		}
		// 获取当前用户集团权限
		List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
		// 获取当前用户地域权限
		List<SiteAllInfo> dyList = loginService.getDySiteList(userInfo);
		// 获取当前用户广场权限
		List<SiteAllInfo> gcList = loginService.getDpGcSiteList(userInfo);
		List<SiteAllInfo> xianzaiList = loginService.getGcSiteList(userInfo);
		for (int i = 0; i < gcList.size(); i++) {
			if (!Tools.isHaveGcPer(xianzaiList, gcList.get(i).getSiteid())) {
				gcList.get(i).setSitestaus(0);
			} else {
				gcList.get(i).setSitestaus(1);
			}
		}
		if (jtList.size() != 0) {
			model.addAttribute("jtList", jtList);
		} else {
			model.addAttribute("jtList", null);
		}
		if (dyList.size() != 0) {
			model.addAttribute("dyList", dyList);
		} else {
			model.addAttribute("dyList", null);
		}
		if (gcList.size() != 0) {
			model.addAttribute("gcList", gcList);
		} else {
			model.addAttribute("gcList", null);
		}
//		// 获取数据类型信息
//		SqlParameter sql = new SqlParameter();
//		sql.setString(userInfo.getUserId());
//		sql.setSysLang(sysLang);
//		List<WordBook> dataList = userService.getDataPermission(sql);
		// 获取数据类型（新版，2016年1月11日10:42:50）
		Map<String, Object> indexMap = userService.selectUserDateType(userInfo
				.getUserId());
		/****** 筛选广场初始化 *****/
		List<SiteAllInfo> dpGcList = loginService.getDpGcSiteList(userInfo);
		ChangeGcParam changeGcParam = new ChangeGcParam();
		changeGcParam.setSelectType("1");// 多选场所
		List<SiteAllInfo> yuanGcList = loginService.getGcSiteList(userInfo);
		SquareChange squareChange = selectSiteService.squareChangeLoad(
				changeGcParam, dpGcList, yuanGcList);// 获取初始化更换广场信息
		model.addAttribute("squareChange", squareChange);// 更换广场数据初始化
		/****** 筛选广场初始化 *****/
		session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 角色下拉框数据
//		model.addAttribute("dataList", dataList);// 数据权限复选框
		model.addAttribute("dataTypeList", JSONArray.fromObject(indexMap
				.get("dataTypeList")));// 数据类型
		model.addAttribute("dataTypeMap", indexMap.get("dataTypeList"));
		model.addAttribute("indexList", JSONArray.fromObject(indexMap
				.get("indexList")));// 指标
		model.addAttribute("userManagerInfo", userManagerInfo);// 要编辑的用户信息
		return "userManagerEdit";
	}

	/**
	 * 编辑用户提交
	 * 
	 * @param userManagerInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userEditSub")
	public String userEditSub(UserManagerInfo userManagerInfo, ModelMap model)
			throws Exception {
		userManagerInfo.setCreateUserId(session.getAttribute("userName")
				.toString());
		MessageCommon messageCommon = userService.editUser(userManagerInfo);
		// 如果发生错误
		if (!messageCommon.isFlag()) {
			return "error";
		}
		// 返回一览页面
		else {
			// 获取当前系统语言
			String sysLang = session.getAttribute("sysLang").toString();
			UserInfo userInfo = (UserInfo) session
					.getAttribute("sessionUserName");
			String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
			// 获取用户信息
			userInfo = loginService.getUserInfo(userInfo);
			// 获取用户角色下拉框信息
			List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
			// 获取用户状态下拉框信息
			List<WordBook> statusList = userService.getUserStatusCombo(sysLang);
			// 获取用户语言包
			Language language = Lang.getInfo(sysLang);
			// 查询当前页面id
			SqlParameter sql = new SqlParameter();
			sql.setSysLang(sysLang);
			sql.setString("userManager");
			Integer menuId = selectSiteService.getMenuId(sql);
			// 获取用户权限按钮信息
			List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
					userInfo.getUserId(), menuId);
			// 按钮过滤
			language = filterLanguage(language, buttonList);
			model.addAttribute("language", language);
			PageDataVo pageDataVo = new PageDataVo();
			// 如果是初始化，默认显示第一页
			if (pageDataVo.getCurrentPage() == 0) {
				pageDataVo.setCurrentPage(1);
			}
			// 获取每页显示条数
			pageDataVo.setPageSize(Integer.parseInt(MessageManager
					.getMsg("pageSize")));
			// 设置开始显示条数
			pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
					* pageDataVo.getPageSize());
			// 设置参数
			userManagerInfo = new UserManagerInfo();
			userManagerInfo.setId(userInfo.getId());
			pageDataVo.setSearchParameter(userManagerInfo);
			// 获取当前页用户信息
			pageDataVo = userService.getUserList(pageDataVo);
			session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
			model.addAttribute("roleList", roleList);// 角色下拉框数据
			model.addAttribute("statusList", statusList);// 用户状态下拉框数据
			model.addAttribute("pageDataVo", pageDataVo);// 用户列表分页信息
			model.addAttribute("recentUserName", userInfo.getUserName());
			model.addAttribute("userManagerInfo", userManagerInfo);
			model.addAttribute("message", messageCommon.getMessage());
			return "userManager";
		}
	}

	/**
	 * 删除、更新用户状态
	 * 
	 * @param userManagerInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateUser")
	public String updateUser(UserManagerInfo userManagerInfo, ModelMap model)
			throws Exception {
		MessageCommon messageCommon = userService.updateUser(userManagerInfo);
		if (!messageCommon.isFlag()) {
			return "error";
		} else {
			// 获取当前系统语言
			String sysLang = session.getAttribute("sysLang").toString();
			UserInfo userInfo = (UserInfo) session
					.getAttribute("sessionUserName");
			String menuHtml = loginService.getMenuHtml(userInfo, "userManager");
			// 获取用户信息
			userInfo = loginService.getUserInfo(userInfo);
			// 获取用户角色下拉框信息
			List<TreeHtml> roleList = userService.getUserRoleCombo(userInfo);
			// 获取用户状态下拉框信息
			List<WordBook> statusList = userService.getUserStatusCombo(sysLang);
			// 获取用户语言包
			Language language = Lang.getInfo(sysLang);
			// 查询当前页面id
			SqlParameter sql = new SqlParameter();
			sql.setSysLang(sysLang);
			sql.setString("userManager");
			Integer menuId = selectSiteService.getMenuId(sql);
			// 获取用户权限按钮信息
			List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
					userInfo.getUserId(), menuId);
			// 按钮过滤
			language = filterLanguage(language, buttonList);
			model.addAttribute("language", language);
			PageDataVo pageDataVo = new PageDataVo();
			// 如果是初始化，默认显示第一页
			if (pageDataVo.getCurrentPage() == 0) {
				pageDataVo.setCurrentPage(1);
			}
			// 获取每页显示条数
			pageDataVo.setPageSize(Integer.parseInt(MessageManager
					.getMsg("pageSize")));
			// 设置开始显示条数
			pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
					* pageDataVo.getPageSize());
			// 设置参数
			userManagerInfo = new UserManagerInfo();
			userManagerInfo.setId(userInfo.getId());
			pageDataVo.setSearchParameter(userManagerInfo);
			// 获取当前页用户信息
			pageDataVo = userService.getUserList(pageDataVo);
			session.setAttribute("menuHtml", menuHtml);// 左侧菜单html
			model.addAttribute("roleList", roleList);// 角色下拉框数据
			model.addAttribute("statusList", statusList);// 用户状态下拉框数据
			model.addAttribute("pageDataVo", pageDataVo);// 用户列表分页信息
			model.addAttribute("userManagerInfo", userManagerInfo);
			model.addAttribute("recentUserName", userInfo.getUserName());
			model.addAttribute("message", messageCommon.getMessage());
			return "userManager";
		}
	}

	/**
	 * 判断用户名是否存在
	 * 
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userSearch")
	@ResponseBody
	public Map<String, Object> userSearch(String userName) throws Exception {
		boolean flag = userService.selectUserExists(userName);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", flag);
		map.put("message", "用户名已存在，请重新输入！");
		return map;
	}

	/**
	 * 判断创建用户数是否超标
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/checkMaxAccount")
	@ResponseBody
	public Map<String, Object> checkMaxAccount() throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", userService.checkMaxAccount(userInfo));
		map.put("message", "当前账户创建用户数量已达最大值！");
		return map;
	}

}
